package com.vren.software.objectdetectionapi.controller;

import com.vren.software.objectdetectionapi.core.domain.ResponseResult;
import com.vren.software.objectdetectionapi.domain.dto.Option;
import com.vren.software.objectdetectionapi.domain.vo.DetectionResult;
import com.vren.software.objectdetectionapi.service.FindContoursService;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/")
public class FindContoursController {


    @Autowired
    private FindContoursService findContoursService;

    @RequestMapping(value = "/find", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
    @ResponseBody
    public byte[] find(@RequestParam("path") String path) {
        Mat mat = findContoursService.find(path);
        MatOfByte matOfByte = new MatOfByte();
        Imgcodecs.imencode(".png", mat, matOfByte);
        return matOfByte.toArray();
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
    @ResponseBody
    public byte[] test(@RequestParam("path") String path) {
        Mat mat = findContoursService.test(path);
        MatOfByte matOfByte = new MatOfByte();
        Imgcodecs.imencode(".png", mat, matOfByte);
        return matOfByte.toArray();
    }

    @RequestMapping(value = "/detection", method = RequestMethod.POST)
    public ResponseResult<DetectionResult> detection(Option option) throws Exception {
        return ResponseResult.success("获取成功", findContoursService.detection(option));
    }
}
